package com.nlp.visualization.controller.spring.config;

/**
 * @author LXM
 * @Title: NLPVisualizationTools
 * @Description:
 * @date 2018/5/16下午4:04
 */

import com.nlp.visualization.pojo.db.TApiStatistics;
import com.nlp.visualization.service.backEnd.IApiCountService;
import com.nlp.visualization.service.impl.backend.ApiCountServiceImpl;
import com.nlp.visualization.utils.TimeHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.context.support.XmlWebApplicationContext;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class ApiFilter implements Filter {

    private Logger logger = LoggerFactory.getLogger(APIHandlerIntercepter.class);

    private static final String SEG = "词法分析接口";

    private static final String SEN = "句法分析接口";

    private static final String DISCOURSE = "语篇分析接口";

    private static final String WEIBO = "微博分析接口";

    @Autowired
    private IApiCountService apiCountService;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        ServletContext sc = filterConfig.getServletContext();
        XmlWebApplicationContext cxt = (XmlWebApplicationContext) WebApplicationContextUtils.getWebApplicationContext(sc);

        if (cxt != null && cxt.getBean("apiCountService") != null && apiCountService == null)
            apiCountService = (IApiCountService) cxt.getBean("apiCountService");
    }

    @Override
    public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) arg0;
        HttpServletResponse reponse = (HttpServletResponse) arg1;
        String requestURI = request.getRequestURI();
        if (requestURI.indexOf("api/") > 0) {

            TApiStatistics tApiStatistics = new TApiStatistics();

            if (requestURI.indexOf("segment") > 0) {
                tApiStatistics.setName(SEG);

            } else if (requestURI.indexOf("sen") > 0) {
                tApiStatistics.setName(SEN);


            } else if (requestURI.indexOf("discourse") > 0) {
                tApiStatistics.setName(DISCOURSE);

            } else if (requestURI.indexOf("weibo") > 0) {
                tApiStatistics.setName(WEIBO);

            } else {
                tApiStatistics.setName("api");
            }
            tApiStatistics.setUri(requestURI);
            tApiStatistics.setIp(request.getLocalAddr());
            tApiStatistics.setTime(TimeHelper.getTime().longValue());
            try {
                apiCountService.save(tApiStatistics);
            } catch (Exception e) {
                e.printStackTrace();
            }
            logger.info(request.getLocalAddr() + "访问了:" + tApiStatistics.getName());
        }
        filterChain.doFilter(arg0, arg1);


    }

    @Override
    public void destroy() {

    }
}
